﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using CRMFramework;

namespace CRMBiz
{
    public static class OutSourceLenderHandler
    {
        public static DataSet GetOutSourceLenderList(int pageIndex, int pageSize)
        {
            DataSet ds;
            try
            {
                var sbSql = new StringBuilder("SELECT *,(select count(*) from crm_outsourcelender) as Total from crm_outsourcelender order by LenderID");
                sbSql.Append(" LIMIT ").Append((pageIndex - 1) * pageSize).Append(", ").Append(pageSize);
                ds = DBhelper.ExecuteDataset(sbSql.ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return ds;
        }

        public static int AddOutSourceLender(string OutSourceLender)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@OutSourceLender", OutSourceLender),
                };

                string sbSql = "Select * From crm_outsourcelender Where OutSourceLender = @OutSourceLender";
                DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "insert into crm_outsourcelender(OutSourceLender) values(@OutSourceLender)";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        public static int EditOutSourceLender(int LenderID, string OutSourceLender)
        {
            int intReturnValue = 0;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@LenderID", LenderID),
                    DBhelper.SetParameter("@OutSourceLender", OutSourceLender),
                };

                string sbSql = "Select * From crm_outsourcelender Where LenderID <> @LenderID And  OutSourceLender = @OutSourceLender";
                DataSet ds = DBhelper.ExecuteDataset(sbSql, para);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    intReturnValue = 0;
                }
                else
                {
                    sbSql = "update crm_outsourcelender set OutSourceLender = @OutSourceLender where LenderID = @LenderID";
                    DBhelper.ExecuteNonQuery(sbSql, para);
                    intReturnValue = 1;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return intReturnValue;
        }

        public static bool DeleteOutSourceLender(int LenderID)
        {
            bool flag = false;
            try
            {
                var para = new[] {
                    DBhelper.SetParameter("@LenderID", LenderID),
                };

                string sbSql = "delete from crm_outsourcelender where LenderID=@LenderID";
                DBhelper.ExecuteNonQuery(sbSql, para);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
